package com.dycong.common.leetcode.yi;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: yilin
 * Date: 2019-07-16
 * Time: 14:05
 */
public class RerverseList {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args) {
        ListNode l1 = new ListNode(1);
        l1.next = new ListNode(2);
        l1.next.next = new ListNode(3);
        l1.next.next.next = new ListNode(4);
        l1.next.next.next.next = new ListNode(5);
        ListNode result = reverseList(l1);
        while (result != null) {
            System.out.print(result.val + "\t");
            result = result.next;
        }
    }

    public static ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode pNext = head.next;
        ListNode reverNode = reverseList(pNext);
        head.next = null;
        pNext.next = head;
        return reverNode;

    }
}
